﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.ApplicationService.Manager;
using System.Text;
using KLS.PPDS.Utility;
using KLS.PPDS.Resources;
using KLS.PPDS.Utilities.Attributes;
using KLS.PPDS.Utilities;
using System.IO;

namespace KLS.PPDS.Web.Models
{
    /// <summary>
    /// viewModel for Article will hold data for Article view
    /// </summary>
    public class ArticleViewModel
    {
        /// <summary>
        /// defaut constructor
        /// </summary>
        public ArticleViewModel()
        {

        }

        /// <summary>
        /// publication for which Article needs to be generated
        /// </summary>

        public Publication Publication { get; set; }

        /// <summary>
        /// all the publication 
        /// </summary>
        public SelectList PublicationList { get; set; }

        /// <summary>
        /// extend1 value list
        /// </summary>
        public SelectList Extend1List { get; set; }


        /// <summary>
        /// Extend2valueList
        /// </summary>
        public SelectList Extend2List { get; set; }

        /// <summary>
        /// all the page number 
        /// </summary>
        public SelectList StartPageList { get; set; }

        /// <summary>
        /// Extent1
        /// </summary>
        public string Extent1 { get; set; }

        /// <summary>
        /// Extent2
        /// </summary>
        public string Extent2 { get; set; }
        /// <summary>
        /// Current Article
        /// </summary>
        public Article Article { get; set; }

        /// <summary>
        /// OldIsReadyForEditor
        /// </summary>
        public bool OldIsReadyForEditor { get; set; }

        /// <summary>
        /// OldIsReadyForGraphic
        /// </summary>
        public bool OldIsReadyForGraphic { get; set; }

        /// <summary>
        /// StartPage
        /// </summary>
        public int? StartPage { get; set; }

        /// <summary>
        /// PerviousPublicationId
        /// </summary>
        public int PerviousPublicationId { get; set; }
        /// <summary>
        /// ListArticleContent
        /// </summary>
        public List<ArticleContent> ListArticleContent { get; set; }

        /// <summary>
        /// SupportedTexFilesExt
        /// </summary>
        public string SupportedTextFilesExt
        {
            get
            {
                return UploadFileTypeManager.GetSupportedTextFileExtForUploadify();
            }
        }
        /// <summary>
        /// SupportedImageFilesExt
        /// </summary>
        public string SupportedImageFilesExt
        {
            get
            {
                return UploadFileTypeManager.GetSupportedImageFileExtForUploadify();
            }
        }

        /// <summary>
        /// SizeLimit
        /// </summary>
        public int SizeLimit 
        {
            get 
            {
                return (Int32)WebConfig.MaxUploadFileSizeMB * 1048576;
            }
        }

        /// <summary>
        /// IsDisable
        /// </summary>
        public bool IsDisable 
        {
            get 
            {
                try
                {
                    if (this.Article != null)
                    {
                        if (this.Article.IsReadyForGraphic.Equals(true) || (this.Article.IsReadyForEditor.Equals(true) && !(CommonSession.IsUserEditor || CommonSession.IsUserSuperAdmin)))
                        {
                            return true;
                        }
                    }
                }
                catch (Exception )
                {
                                        
                }
                return false;
            }
        }
        
        /// <summary>
        /// IsGraphicsDisable
        /// </summary>
        public bool IsGraphicsDisable
        {
            get
            {
                try
                {
                    if (this.Article != null)
                    {
                        if (this.Article.IsReadyForGraphic.Equals(true) || CommonSession.IsUserGraphicsDesigner)
                        {
                            return true;
                        }
                    }
                }
                catch (Exception )
                {
                    
                    
                }
                return false;
            }
        }

        /// <summary>
        /// IsTextDisable
        /// </summary>
        public bool IsTextDisable
        {
            get
            {
                try
                {
                    if (this.Article != null)
                    {
                        if (this.Article.IsReadyForEditor.Equals(true) && !(CommonSession.IsUserSuperAdmin || CommonSession.IsUserEditor))
                        {
                            return true;
                        }
                        else if (this.Article.IsReadyForEditor.Equals(false) && CommonSession.IsUserGraphicsDesigner)
                        {
                            return true;
                        }
                        else if (this.Article.IsReadyForGraphic.Equals(true))
                        {
                            return true;
                        }
                    }
                }
                catch (Exception )
                {
                    
                    
                }
                return false;
            }
        }

        /// <summary>
        /// ReadyForEditorPermission
        /// </summary>
        public bool ReadyForEditorPermission 
        {
            get 
            {
                try
                {
                    if (this.Article != null && (CommonSession.IsUserSuperAdmin || CommonSession.IsUserJournalist || CommonSession.IsUserEditor) && this.Article.IsReadyForEditor.Equals(false))
                    {
                        return true;
                    }
                }
                catch (Exception )
                {
                    
                    
                }

                return false;
            }
        }

        /// <summary>
        /// ReadyForGraphicsPermission
        /// </summary>
        public bool ReadyForGraphicsPermission
        {
            get
            {
                try
                {
                    // this is blocked because superadmin can set ready for editor & graphic same time 
                    //if ((CommonSession.IsUserSuperAdmin || CommonSession.IsUserEditor) && this.ReadyForEditorPermission.Equals(true)) 
                    //{
                    //    return false;
                    //}
                    if (this.Article != null && (CommonSession.IsUserSuperAdmin || CommonSession.IsUserEditor) && this.Article.IsReadyForGraphic.Equals(false))
                    {
                        return true;
                    }
                }
                catch (Exception )
                {
                    
                    
                }

                return false;
            }
        }

        /// <summary>
        /// GetArticleContentViewModelList
        /// </summary>
        /// <returns></returns>
        public List<ArticleContentViewModel> GetArticleContentViewModelList() 
        {
           // var directory = @"C:\Contents\Projects\Bording Data\PPDS_2\KLS.PPDS.Web\App_Data\uploads";

            var list = new List<ArticleContentViewModel>();

            if (this.ListArticleContent != null && this.ListArticleContent.Count > 0)
            {
                var item = new ArticleContentViewModel();
                foreach (var content in this.ListArticleContent)
                {
                    
                    item = new ArticleContentViewModel();
                    item.PublicationId = this.Publication.Id;
                    item.CompanyId = this.Publication.CompanyId;
                    content.CopyPropertyValue<Core.Entity.ArticleContent, ArticleContentViewModel>(item);
                    item.Article = this.Article;
                    item.Images = content.Images;
                    //item.ImageFiles = FileUtil.NewGetImageFiles(item.ImageFilePath);
                    item.TextFiles = FileUtil.NewGetTextFiles(item.TextFilePath);
                    item.Images =  ContentImageManager.GetContentImageByContentId(content.Id);
                    list.Add(item);
                }

            }
            return list;
        }


    }


    
    public class ArticleModel 
    {

        public static string SaveCommnetsText(string comments, int pubId, int artId) 
        {
            var reslt = string.Empty;
            var pub = PublicationManager.GetPublicationById(pubId);

            if (!String.IsNullOrEmpty(comments)) 
            {
                reslt = FileUtil.WriteTextFile(comments, ArticleContentModel.GetArtileBaseFolde(pub.CompanyId, pubId, artId), "Commnets.txt", false);
            }
            
            return reslt;
        }

        public static string DeleteArticle(int id) 
        {
            var art = ArticleManager.GetArticleById(id);
            var pub = PublicationManager.GetPublicationById(art.PublicationId);
            foreach (var artContent in art.Content) 
            {
                ArticleContentManager.Delete(artContent);
            }

            if (art != null && art.Id>0) 
            {
                ArticleManager.Delete(art);
            }

            FileUtil.DeleteFolder(ArticleContentModel.GetArtileBaseFolde(pub.CompanyId, art.PublicationId, art.Id));

            return string.Empty;
        } 

        public static SelectList GetStartPageList(int pubId, int pages, int? startpage)
        {

            var list = new List<int>();

            for (int i = 1; i <= pages; i++)
            {

                for (double y = i; y <= i + 1; y = y + 0.25)
                {
                    if (y != i + 1 && !ArticleManager.IsOccupied(pubId, y, y + 0.25))
                    {
                        if (!list.Contains(i))
                        {
                            list.Add(i);
                            break;
                        }
                    }
                }
            }

            if (startpage.HasValue && startpage.Value > 0)
            {
                if (!list.Contains(startpage.Value)) 
                {
                    list.Add((startpage.Value));
                }
                
                list = list.OrderBy(c => c).ToList();
            }

            return new SelectList(list);
        }

        public static void ProcessIndex(ArticleViewModel model, int publicationId, int articleId, int startPage)
        {
            

            model.Article = articleId == 0 ? new Core.Entity.Article() : ArticleManager.GetArticleById(articleId);
            if (startPage!=0)
            {
                model.Article.StartPage = startPage;
            }

            model.Article.PublicationId = publicationId;
            ProcessModel(model);            
        }

        static void ProcessModel(ArticleViewModel model) 
        {
            model.Publication = PublicationManager.GetPublicationById(model.Article.PublicationId);
            model.PublicationList = new SelectList(PublicationManager.GetAllPublication(model.Publication.CompanyId), "Id", "DdlPublicationName");
            model.Extend1List = new SelectList(PageExtensionManager.GetExtend1List());
            model.Extend2List = new SelectList(PageExtensionManager.GetExtensions(), "ExtensionValue", "ExtensionName");
            model.Extent1 = Convert.ToString( (model.Article.ExtentValue1??0));
            model.Extent2 = Convert.ToString(model.Article.ExtentValue2 ?? 0);
            if (model.Article.StartPage.HasValue && model.Article.StartPage.Value > 0)
            {
                model.StartPage = Convert.ToInt32(Math.Floor(model.Article.StartPage.Value));
            }
            else 
            {
                model.StartPage = null;
            }

            model.StartPageList = GetStartPageList(model.Publication.Id, model.Publication.Pages.GetValueOrDefault(1), model.StartPage);

            model.OldIsReadyForEditor = model.Article.IsReadyForEditor;
            model.OldIsReadyForGraphic = model.Article.IsReadyForGraphic;
            model.PerviousPublicationId = model.Article.PublicationId;

            if (model.Article.Id > 0) 
            {
                model.ListArticleContent = ArticleContentManager.GetArticleContentByArticleId(model.Article.Id);

                if (model.ListArticleContent != null && model.ListArticleContent.Count > 0) 
                {
                    foreach (var item in model.ListArticleContent) 
                    {
                        item.Images = ContentImageManager.GetContentImageByContentId(item.Id);
                    }
                }
            }
        }

        public static string Save(ArticleViewModel model) 
        {

            var errMsg = CommonMessage.OccupiedByAnotherArticle;



            //When only move permission is allowed
            if (model.Article.Id > 0 && String.IsNullOrEmpty(model.Article.Headline))
            {
                model.Article = ArticleManager.GetArticleById(model.Article.Id);
                model.Extent1 = model.Article.ExtentValue1.HasValue.Equals(true) ? model.Article.ExtentValue1.ToString() : "";
                model.Extent2 = model.Article.ExtentValue2.HasValue.Equals(true) ? model.Article.ExtentValue2.ToString() : "";
            }

            if (String.IsNullOrEmpty(model.Extent1) && String.IsNullOrEmpty(model.Extent2))
            {
                return CommonMessage.SelectExtent;
            }

            else if (String.IsNullOrEmpty(model.Extent1) && !String.IsNullOrEmpty(model.Extent2)) 
            {
                model.Article.ExtentValue2 = Convert.ToDouble(model.Extent2);
            }
            else if (!String.IsNullOrEmpty(model.Extent1) && String.IsNullOrEmpty(model.Extent2))
            {
                model.Article.ExtentValue1 = Convert.ToInt32(model.Extent1);
            }
            else if (!String.IsNullOrEmpty(model.Extent1) && !String.IsNullOrEmpty(model.Extent2))
            {
                model.Article.ExtentValue1 = Convert.ToInt32( model.Extent1);
                model.Article.ExtentValue2 = Convert.ToDouble(model.Extent2);
            }

            model.Article.StartPage = model.StartPage;

            if (model.PerviousPublicationId.Equals(model.Article.PublicationId))// (Add/Edit)
            {
                if (ArticleManager.IsAppropiatePositionFound(model.Article))
                {
                    SaveArticle(model);
                    return string.Empty;
                }                
            }
            else // moving rticle to another publication
            {

                var pub = PublicationManager.GetPublicationById(model.PerviousPublicationId);

                var oldF = ArticleContentModel.GetArtileBaseFolde(pub.CompanyId, model.PerviousPublicationId, model.Article.Id);
                var newF = ArticleContentModel.GetArtileBaseFolde(pub.CompanyId, model.Article.PublicationId, model.Article.Id);

                FileUtil.MoveFolder(oldF, newF);

                if (ArticleManager.Validate(model.Article)) // if target page is empty
                {

                    
                    SaveArticle(model);
                    return string.Empty;
                }
                else // if target page is not empty then move to standby page
                {
                    model.Article.StartPage = null;
                    model.Article.EndPage = null;
                    
                    SaveArticle(model);

                    return string.Empty;
                }
            }

            if (model.Article.EndPage > PublicationManager.GetPageCountByPublicationId(model.Article.PublicationId) + 1) 
            {
                errMsg = CommonMessage.ArticleEndPageExceed;
            }

            return errMsg;
            
        }

        /// <summary>
        /// SaveArticle
        /// </summary>
        /// <param name="model"></param>
        static void SaveArticle(ArticleViewModel model) 
        {
            //setp 1. save to db
            ArticleManager.Save(model.Article);

            //setp 2. Generate comments text file
            if (model.Article.Id > 0) 
            {
                SaveCommnetsText(model.Article.Comments, model.Article.PublicationId, model.Article.Id);
            }            

            //setp 3. Create publication pacakge 
            CreatePublicationPackage(model);

            //step 4. Send mail
            SendMail(model);

            //step 5. Process model for new view
            ProcessModel(model);
        }

        //public static void CreatePublicationPackage(ArticleViewModel model) 
        //{
        //    if ( model.OldIsReadyForGraphic.Equals(model.Article.IsReadyForGraphic).Equals(false))
        //    {
        //        var pub = PublicationManager.GetPublicationById(model.Article.PublicationId);
        //        var oldPath = ArticleContentModel.GetArtileBaseFolde(pub.CompanyId, pub.Id, model.Article.Id);
        //        var destPath = ArticleContentModel.GetPublicationPackageArtileBaseFolde(pub.CompanyId, pub.Id, model.Article.Id);
        //        FileUtil.MoveFolder(oldPath, destPath);
        //        FileUtil.CreateDirectory(ArticleContentModel.GetPublicationLayoutFolder(pub.CompanyId, pub.Id));
        //    }
        //}

        
        public static void CreatePublicationPackage(ArticleViewModel model)
        {
            if (model.OldIsReadyForGraphic.Equals(model.Article.IsReadyForGraphic).Equals(false) && model.Article.IsReadyForGraphic)
            {
                var pub = PublicationManager.GetPublicationById(model.Article.PublicationId);

                var comDir = ArticleContentModel.GetPackageCompanyBaseFolder(pub.Company);
                if (!Directory.Exists(comDir))
                    Directory.CreateDirectory(comDir);

                var pubDir = ArticleContentModel.GetPackagePublicationBaseFolder(pub);
                if (!Directory.Exists(pubDir))
                    Directory.CreateDirectory(pubDir);

                var oldPath = ArticleContentModel.GetArtileBaseFolde(pub.CompanyId, pub.Id, model.Article.Id);

                if (Directory.Exists(oldPath))
                {
                    var destPath = ArticleContentModel.GetPublicationPackageArtileBaseFolde(model.Article);
                    //var destPath = ArticleContentModel.GetPublicationPackageArtileBaseFolde(pub.CompanyId, pub.Id, model.Article.Id, model.Article.Headline);
                    var artContentList = ArticleContentManager.GetArticleContentByArticleId(model.Article.Id);
                    string oldSubArtPath=string.Empty;
                    string newSubArtPath=string.Empty;                    
                    string newImageFolder = string.Empty;
                    string oldImageFolder = string.Empty;
                    string oldImagePath = string.Empty;
                    string newImagePath = string.Empty;
                    string newImageName = string.Empty;

                    if (!Directory.Exists(destPath))
                        Directory.CreateDirectory(destPath);
                    foreach (var artContent in artContentList)
                    {
                        oldSubArtPath = ArticleContentModel.GetArtileContentBaseFolder(pub.CompanyId, pub.Id, model.Article.Id, artContent.Id);
                        //newSubArtPath = ArticleContentModel.GetPackageArtileContentBaseFolder(pub.CompanyId, pub.Id, model.Article.Id, model.Article.Headline, artContent.Id, artContent.Headline);
                        newSubArtPath = ArticleContentModel.GetPackageArtileContentBaseFolder(artContent);
                        
                        if (Directory.Exists(oldSubArtPath))
                        {
                            if (!Directory.Exists(newSubArtPath))
                                Directory.CreateDirectory(newSubArtPath);

                            foreach (var fi in new DirectoryInfo(oldSubArtPath).GetFiles("*.*", SearchOption.TopDirectoryOnly))
                            {
                                fi.MoveTo(Path.Combine(newSubArtPath, fi.Name));
                            }

                            var DIs = new DirectoryInfo(oldSubArtPath).GetDirectories("*.*", SearchOption.TopDirectoryOnly);
                            foreach (var di in DIs)
                            {
                                if (!di.Name.ToLower().Equals("images"))
                                    di.MoveTo(Path.Combine(newSubArtPath, di.Name));
                                else
                                {
                                    oldImageFolder = ArticleContentModel.GetArtileContentImageFolder(pub.CompanyId, pub.Id, model.Article.Id, artContent.Id);
                                    var DIIs = new DirectoryInfo(oldImageFolder).GetDirectories("*.*", SearchOption.TopDirectoryOnly);                                    
                                    //newImageFolder = ArticleContentModel.GetPackageArtileContentImageFolder(pub.CompanyId, pub.Id, model.Article.Id, model.Article.Headline, artContent.Id, artContent.Headline);
                                    newImageFolder = ArticleContentModel.GetPackageArtileContentImageFolder(artContent);
                                    if (!Directory.Exists(newImageFolder))
                                        Directory.CreateDirectory(newImageFolder);
                                    foreach (var dii in DIIs)
                                    {
                                        dii.MoveTo(Path.Combine(newImageFolder, dii.Name));
                                    }                                    
                                    
                                    foreach (var image in artContent.Images)
                                    {
                                        oldImagePath = Path.Combine(oldImageFolder, image.DiskFileName);
                                        if (File.Exists(oldImagePath))
                                        {                                             
                                            newImageName = image.ImageIndex.ToString() + "_" + image.Name.Remove(image.Name.LastIndexOf('.'));
                                            if (image.Cutout)
                                                newImageName = newImageName + "_Cutout";
                                            if (image.Retouch)
                                                newImageName = newImageName + "_Retouch";
                                            newImageName = newImageName + Path.GetExtension(oldImagePath);
                                            newImagePath = Path.Combine(newImageFolder, newImageName);
                                            File.Move(oldImagePath, newImagePath);
                                            image.DiskFileName=newImageName;
                                            ContentImageManager.Save(image);
                                        }
                                    }
                                }
                            }                            
                        }

                        Directory.Delete(oldSubArtPath,true);

                        oldSubArtPath = string.Empty;
                        newSubArtPath = string.Empty;
                    }

                    
                    foreach (var fi in new DirectoryInfo(oldPath).GetFiles("*.*", SearchOption.TopDirectoryOnly))
                    {
                        fi.MoveTo(Path.Combine(destPath, fi.Name));
                    }

                    FileUtil.CreateDirectory(ArticleContentModel.GetPublicationLayoutFolder(pub));
                    Directory.Delete(oldPath,true);
                }
            }
        }


        public static void SendMail(ArticleViewModel model)
        {
            var editor = ArticleManager.GetEditorUserByPublicationId(model.Article.PublicationId);
            var graphics = ArticleManager.GetGraphicsUserByPublicationId(model.Article.PublicationId);            

            if (!model.OldIsReadyForEditor.Equals(model.Article.IsReadyForEditor) && editor != null && editor.Id > 0)
            {
                SendMailToEditor(model, editor);
            }

            if (!model.OldIsReadyForGraphic.Equals(model.Article.IsReadyForGraphic) && graphics != null && graphics.Id > 0)
            {
                SendMailToGraphics(model, graphics);
            }
        }

        public static void SendMailToEditor(ArticleViewModel model, User editor)
        {
            var body = new StringBuilder();
            var subject = new StringBuilder();

            model.Publication = PublicationManager.GetPublicationById(model.Article.PublicationId);

            var extentValueString = "";

            var startPageValueString = model.Article.StartPage.HasValue ? ((int)Math.Floor(model.Article.StartPage.Value)).ToString() : "Standby";

            //var startPageValueString = model.Article.StartPage.HasValue ? ((int)model.Article.StartPage.Value).ToString() + PageExtensionManager.GetExtensionsStringByValue(model.Article.StartPage.Value - (int)model.Article.StartPage.Value) : "Standby";

            if (model.Article.ExtentValue1.HasValue)
                extentValueString = model.Article.ExtentValue1.Value.ToString();
            if (model.Article.ExtentValue2.HasValue)
                extentValueString = extentValueString + PageExtensionManager.GetExtensionsStringByValue(model.Article.ExtentValue2.Value);

            //if (Utility.WebConfig.EmailFormatIsHtml)
                //body.Append("<!DOCTYPE HTML><html><head><title>E2A Mail</title><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /></head><body><table><tr><td>");
                body.Append(CommonTask.MailBodyStart);

            //var newLine = Utility.WebConfig.EmailFormatIsHtml == true ? "<br/>" : Environment.NewLine;
                var newLine = CommonTask.MailBodyNewLine;

            subject.AppendFormat("Article {0} is ready for editor.", model.Article.Headline);

            body.AppendFormat("Hi {0},", editor.UserFullName ?? "Editor");
            body.Append(newLine);
            body.Append(newLine);
            body.AppendFormat("Article \"{0}\" (ID={1}) is ready for you. Please check.", model.Article.Headline, model.Article.Id);
            body.Append(newLine);
            body.Append(newLine);
            body.Append("Article details: ");
            body.Append(newLine);
            body.AppendFormat("Publication: {0} (ID={1})", model.Publication.PublicationName, model.Publication.Id);
            body.Append(newLine);
            body.AppendFormat("Number: {0}", model.Publication.PublicationNumber);
            body.Append(newLine);
            body.AppendFormat("Extent: {0}", extentValueString);
            body.Append(newLine);            
            body.AppendFormat("Start page: {0}", startPageValueString);
            body.Append(newLine);
            body.Append(newLine);
            body.Append(newLine);
            body.Append("Regards,");
            body.Append(newLine);
            body.Append("E2A");
            body.Append(newLine);
            body.Append(newLine);
            body.Append("Note: This is an auto generated mail.");

            //if (Utility.WebConfig.EmailFormatIsHtml)
            body.Append(CommonTask.MailBodyEnd);
            if (subject.Length > 0)
            {
                CommonTask.SendMail(WebConfig.DefaultEmailSender, editor.UserName, subject.ToString(), body.ToString());
            }
        }

        public static void SendMailToGraphics(ArticleViewModel model, User graphics)
        {
            var body = new StringBuilder();
            var subject = new StringBuilder();

            model.Publication = PublicationManager.GetPublicationById(model.Article.PublicationId);

            var extentValueString = "";

            var startPageValueString = model.Article.StartPage.HasValue ? ((int)Math.Floor(model.Article.StartPage.Value)).ToString() : "Standby";

            //var startPageValueString = model.Article.StartPage.HasValue ? ((int)model.Article.StartPage.Value).ToString() + PageExtensionManager.GetExtensionsStringByValue(model.Article.StartPage.Value - (int)model.Article.StartPage.Value) : "Standby";

            if (model.Article.ExtentValue1.HasValue)
                extentValueString = model.Article.ExtentValue1.Value.ToString();
            if (model.Article.ExtentValue2.HasValue)
                extentValueString = extentValueString + PageExtensionManager.GetExtensionsStringByValue(model.Article.ExtentValue2.Value);

            //if (Utility.WebConfig.EmailFormatIsHtml)
            //    body.Append("<!DOCTYPE HTML><html><head><title>E2A Mail</title><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /></head><body><table><tr><td>");
            body.Append(CommonTask.MailBodyStart);
            var newLine = CommonTask.MailBodyNewLine;
            //var newLine = Utility.WebConfig.EmailFormatIsHtml == true ? "<br/>" : Environment.NewLine;
            subject.AppendFormat("Article {0} is ready for graphics.", model.Article.Headline);

            body.AppendFormat("Hi {0},", graphics.UserFullName ?? "Graphics Designer");
            body.Append(newLine);
            body.Append(newLine);
            body.AppendFormat("Article \"{0}\" (ID={1}) is ready for you. Please check.", model.Article.Headline, model.Article.Id);
            body.Append(newLine);
            body.Append(newLine);
            body.Append("Article details: ");
            body.Append(newLine);
            body.AppendFormat("Publication: {0} (ID={1})", model.Publication.PublicationName, model.Publication.Id);
            body.Append(newLine);
            body.AppendFormat("Number: {0}", model.Publication.PublicationNumber);
            body.Append(newLine);
            body.AppendFormat("Extent: {0}", extentValueString);
            body.Append(newLine);
            body.AppendFormat("Start page: {0}", startPageValueString);            
            body.Append(newLine);
            body.AppendFormat("Path: {0}", Path.Combine(Utility.WebConfig.FtpRootPath, ArticleContentModel.GetPublicationPackageArtileBaseFolde(model.Article).Replace(WebConfig.PublicationPackageFolder + @"\", "").Replace(@"\", "/")));
            body.Append(newLine);
            body.Append(newLine);
            body.Append(newLine);
            body.Append("Regards,");
            body.Append(newLine);
            body.Append("E2A");
            body.Append(newLine);
            body.Append(newLine);
            body.Append("Note: This is an auto generated mail.");

            //if (Utility.WebConfig.EmailFormatIsHtml)
            //    body.Append("</td></tr></table></body></html>");
            body.Append(CommonTask.MailBodyEnd);
            if (subject.Length > 0)
            {
                CommonTask.SendMail(WebConfig.DefaultEmailSender, graphics.UserName, subject.ToString(), body.ToString());
            }
        }        
    }

}